# 1. 目标网站
# https://www.doyo.cn/passport/login?next=https://www.doyo.cn/
# 2. js代码
import hashlib

...

# 3. 获取token
# https://www.doyo.cn/User/Passport/token?username=dyl12345&random=0.6288276394413997
import requests
from fake_useragent import UserAgent


def Sha1(password):
    """使用Python SHA1算法"""
    return hashlib.sha1(password.encode("utf-8")).hexdigest()


url = "https://www.doyo.cn/User/Passport/token?username=dyl12345&random=0.6288276394413997"
headers = {
    "User-Agent": UserAgent().random
}

username = "dyl12345"
password = "123456"

params = {
    "username": "dyl12345",
    "random": 0.6288276394413997
}
res = requests.get(url=url, headers=headers, params=params)
nonce = res.json().get("nonce")
ts = res.json().get("ts")
next = "aHR0cHMlM0ElMkYlMkZ3d3cuZG95by5jbiUyRg=="
pwd = Sha1(password)
pwd = Sha1(str(nonce) + str(ts) + pwd)
params = {
    "username": "dyl12345",
    "password": pwd,
    "remberme": 1,
    "next": next
}
url = "https://www.doyo.cn/User/Passport/login"
res = requests.post(url=url, headers=headers, params=params)
print(res.text)

"""
$(".login_submit").click(function(){
	$(".login_grey[name='user_name']").triggerHandler("blur");
	$(".login_grey[name='user_password']").triggerHandler("blur");
	if($(".login_red").length==0){
		var nonce=null;
		var ts=null;
		var pwd=null;
		$.get("/User/Passport/token",{"username":$(".login_grey[name='user_name']").val(),"random":Math.random()},function(data){
			if(data.result) {
				nonce = data.nonce;
				ts = data.ts;
				pwd = Sha1.hash($(".login_grey[name='user_password']").val());
				pwd = Sha1.hash(nonce+ts+pwd);
				$.post("/User/Passport/login",{"username":$(".login_grey[name='user_name']").val(),
					"password":pwd,
					"remberme":$(".login_keep[name='keep_login']:checked").length,
					"next":"aHR0cHMlM0ElMkYlMkZ3d3cuZG95by5jbiUyRg=="},function(data){
					if(data.result){
						location=data.next;
					}else{
						if(data.reason=="您的注册邮箱还没通过验证！"){
							location="/User/Passport/register_2?username="+data.username+"&email="+data.email;
						}else{
							$(".login_box .red").hide();
							$(".login_box .alarm_login").addClass("red").show().html(data.reason);
						}
					}
				},"json");

			}else{
				alert("加密字符串获取失败！");
			}
		},"json");
	}
});
"""
